package com.gree.changying.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.gree.changying.R;
import com.gree.changying.utils.ActivityCollector;

/**
 * Create by 游德禄 on 2019-9-16
 * 由下往上推出窗口
 */
public class PopWindow {
    private PopupWindow popWin = null; // 弹出窗口
    public View view = null; // 保存弹出窗口布局
    private LayoutInflater inflater;
    private int position = 0 ;
    private OnDismiss onDismiss;
    private boolean backgroundDarken = true;
    public PopWindow(Context context, int r){
        this(context,r,true);
    }
    public PopWindow(Context context, int r, boolean marchParent){
        this.inflater = LayoutInflater.from(context);
        this.view = inflater.inflate(r, null);
        this.popWin = new PopupWindow(view,
                marchParent? LayoutParams.MATCH_PARENT: LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); // 实例化PopupWindow
        this.popWin
                .setAnimationStyle(R.style.popupAnimation);
        this.popWin.setBackgroundDrawable(new ColorDrawable());
        popWin.setOnDismissListener(new poponDismissListener());
    }
    public void setBackgroundDarken(boolean darken){
        this.backgroundDarken = darken;
    }
    public void setOutsideTouchable(boolean touchable){
        popWin.setOutsideTouchable(touchable);
        popWin.update();
    }
    public void setTouchable(boolean touchable){
        popWin.setTouchable(touchable);
        popWin.update();
    }
    public void setFocusable(boolean focusable){
        popWin.setFocusable(focusable);
        popWin.update();
    }
    public com.gree.changying.widget.PopWindow setOnDissmissListener(OnDismiss onDismiss){
        this.onDismiss = onDismiss;
        return this;
    }
    public interface OnDismiss{
        void dismiss();
    }
    /**
     * 获得布局
     * @return
     */
    public View getView(){
        return view;
    }
    /**
     * 获得打开时的位置
     * @return
     */
    public int getPosition(){
        return position;
    }
    /**
     * 显示
     * @param parentView
     */
    public void  show(View parentView){
        this.popWin.showAtLocation(parentView, Gravity.BOTTOM, 0, 0); // 显示弹出窗口
        backgroundAlpha(0.5f);
    }
    /**
     * 显示
     * @param parentView
     */
    public void show(View parentView, int position){
        show(parentView,position, Gravity.BOTTOM);
    }
    public void show(View parentView, int position, int gravity){
        this.position = position;
        this.popWin.showAtLocation(parentView, gravity, 0, 0); // 显示弹出窗口
        backgroundAlpha(0.5f);
    }
    /**
     * 关闭
     */
    public void dismiss(){
        popWin.dismiss();
    }

    class poponDismissListener implements PopupWindow.OnDismissListener {

        @Override
        public void onDismiss() {

            backgroundAlpha(1f);
            if(onDismiss!=null){
                onDismiss.dismiss();
            }
        }
    }
    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        if(backgroundDarken) {
            Activity activity = ActivityCollector.currentActivity();
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.alpha = bgAlpha;
            activity.getWindow().setAttributes(lp);
        }
    }
}
